<?php
class PantsTag extends PantsObject {
  public $id;
  public $name;
  
  private $articleCount = null;

  // -- Public Static Methods --------------------------------------------------
  public static function createLink($article_id, $tagName) {
    $tag = PantsTag::getByName($tagName);

    if ($tag->id == 0) {
      $tag->save();
    }

    return Pants::query('tags.createLink', array(
      'article_id' => $article_id,
      'tag_id'     => $tag->id
    ));
  }

  public static function deleteLinks($article_id) {
    return Pants::query('tags.deleteLinks', array(
      'article_id' => $article_id
    ));
  }

  public static function getAll() {
    $result = Pants::query('tags.getAll');

    $tags = array();

    while($result && $row = $result->fetch_array()) {
      $tags[] = PantsTag::load($row);
    }

    return $tags;
  }

  public static function getByArticleId($article_id) {
    $result = Pants::query('tags.getByArticleId',
        array('article_id' => $article_id));

    $tags = array();

    while ($result && $row = $result->fetch_array()) {
      $tags[] = PantsTag::load($row);
    }

    return $tags;
  }

  public static function getById($id) {
    $result = Pants::query('tags.getById', array('id' => $id));

    if (!$result) {
      throw new PantsException('Invalid tag id.');
    }

    return PantsTag::load($result->fetch_array());
  }

  public static function getByName($name) {
    $result = Pants::query('tags.getByName', array('name' => $name));

    if ($result && $row = $result->fetch_array()) {
      return PantsTag::load($row);
    }

    return new PantsTag(0, $name);
  }

  public static function getNamesByArticleId($article_id) {
    $result = Pants::query('tags.getByArticleId', array(
      'article_id' => $article_id
    ));

    $tagNames = array();

    while($result && $row = $result->fetch_array()) {
      $tagNames[] = $row['name'];
    }

    return $tagNames;
  }

  public static function getRecent($limit = 10) {
    $result = Pants::query('tags.getRecent', array('limit' => $limit));

    $tagNames = array();

    while($result && $row  = $result->fetch_array()) {
      $tagNames[] = $row['name'];
    }

    return $tagNames;
  }

  public static function load($row) {
    return new PantsTag($row['id'], $row['name']);
  }

  public static function search($query) {
    $result = Pants::query('tags.search', array('query' => $query));

    $tags = array();

    while($result && $row = $result->fetch_array()) {
      $tags[] = PantsTag::load($row);
    }

    return $tags;
  }

  // -- Public Instance Methods ------------------------------------------------
  public function __construct($id = 0, $name = '') {
    $this->id   = $id;
    $this->name = $name;
  }

  public function getArticleCount() {
    if (!is_null($this->articleCount)) {
      return $this->articleCount;
    }

    $result = Pants::query('tags.getArticleCount', array('id' => $this->id));

    if ($result && $row = $result->fetch_array()) {
      $this->articleCount = $row[0];
    }
    else {
      $this->articleCount = 0;
    }

    return $this->articleCount;
  }

  public function getArticles($limit = 10, $offset = 0) {
    return PantsArticle::getByTagId($this->id, $limit, $offset);
  }

  public function save() {
    if ($this->id == 0) {
      $result = Pants::query('tags.create', array('name' => $this->name));

      if ($result) {
        $this->id = Pants::$db->getInsertId();
      }
    }
    else {
      $result = Pants::query('tags.update', array(
        'id'   => $this->id,
        'name' => $this->name
      ));
    }

    return $result;
  }
}
?>